package com.zwkj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwkj.common.R;
import com.zwkj.domain.WFileVersionT;
import com.zwkj.domain.WFileWatermarkT;
import com.zwkj.exception.InvalidArgument;
import com.zwkj.pojo.vo.FileVo;
import com.zwkj.pojo.vo.WFileWatermarkTVo;
import com.zwkj.service.IWFileVersionTService;
import com.zwkj.service.IWFileWatermarkTService;
import com.zwkj.service.impl.WFileTServiceImpl;
import org.apache.tomcat.jni.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Gap
 * @version 1.0
 * @createDate 时间:2023/12/13
 * @description 描述:
 */
@RestController
@RequestMapping("")
public class ExtendCapacityController {

    @Autowired
    private IWFileVersionTService versionTService;
    @Autowired
    private WFileTServiceImpl fileTService;
    @Autowired
    private IWFileWatermarkTService wFileWatermarkTService;


    /**
     * 文档重命名
     *
     * @param fileId
     * @param fileVo
     * @return
     */
    @PutMapping(value = "/v3/3rd/files/{file_id}/name")
    public R<?> fileRename(@PathVariable("file_id") String fileId, @RequestBody FileVo fileVo) {
        String name = fileVo.getName();
        if (name == null || name.trim().equals("")) {
            throw new InvalidArgument("new filename is empty");
        }
        fileTService.fileRename(fileId, name);
        return R.ok();
    }

    /**
     * 文档历史版本列表-获取文件历史版本列表
     *
     * @param fileId
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping(value = "/v3/3rd/files/{file_id}/versions")
    public R<?> fileVersions(@PathVariable("file_id") String fileId,
                             @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
                             @RequestParam(value = "limit", required = false, defaultValue = "100") int limit) {
        List<FileVo> fileVoList = versionTService.fileVersions(fileId, offset, limit);
        return R.ok(fileVoList);
    }

    /**
     * 获取文档指定历史版本
     *
     * @param fileId
     * @param version
     * @return
     */
    @GetMapping(value = "/v3/3rd/files/{file_id}/versions/{version}")
    public R<?> fileVersionByVersion(@PathVariable("file_id") String fileId,
                                     @PathVariable("version") int version) {
        FileVo fileVo = versionTService.fileVersionByVersion(fileId, version);
        return R.ok(fileVo);
    }


    /**
     * 获取历史版本下载信息
     *
     * @param fileId
     * @param version
     * @return
     */
    @GetMapping(value = "/v3/3rd/files/{file_id}/versions/{version}/download")
    public R<?> fileVersionDownload(@PathVariable("file_id") String fileId, @PathVariable("version") int version) {
        FileVo fileVo = versionTService.fileVersionDownload(fileId, version);
        return R.ok(fileVo);
    }


    /**
     * 文档水印
     * @param fileId
     * @return
     */
    @RequestMapping(value = "/v3/3rd/files/{file_id}/watermark")
    public R<?> fileWatermark(@PathVariable("file_id") String fileId) {
        WFileWatermarkTVo watermarkTVo = wFileWatermarkTService.fileWatermark(fileId);
        return R.ok(watermarkTVo);
    }
}
